import os

import click
from flask import Flask, render_template

from blueBlog.extensions import db, moment, mail, bootstrap, ckeditor
from blueBlog.models import Admin, Post, Category, Comment
from blueBlog.settings import config

from blueBlog.blueprints.admin import admin_bp
from blueBlog.blueprints.auth import auth_bp
from blueBlog.blueprints.blog import blog_bp
# app.register_blueprint(auth_bp, url_prefix="/auth")

basedir = os.path.abspath(os.path.dirname(os.path.dirname(__file__)))

def create_app(config_name=None):
    if config_name is None:
        config_name = os.getenv('FLASK_CONFIG', 'development')

    app = Flask("blueBlog")
    app.config.from_object(config[config_name])

    register_logging(app)
    register_extensions(app)
    register_blueprints(app)
    register_commands(app)
    register_errors(app)
    register_shell_context(app)
    register_template_context(app)
    return app

    # register_logging(app) # 注册日志
    # register_extensions(app) # 注册扩展
    # register_blueprints(app) # 注册蓝图
    # register_commands(app) # 注册命令行
    # register_commands(app) # 注册命令行
    # register_errors(app) # 注册错误
    # register_shell_context(app) # 注册flask shell上下行
    # register_template_context(app) # 注册渲染模板

    return app


def register_logging(app):
    pass


def register_extensions(app):
    bootstrap.init_app(app)
    db.init_app(app)
    mail.init_app(app)
    moment.init_app(app)
    ckeditor.init_app(app)


def register_blueprints(app):
    app.register_blueprint(blog_bp)
    app.register_blueprint(admin_bp, url_prefix='/admin')
    app.register_blueprint(auth_bp, url_prefix='/auth')


def register_shell_context(app):
    @app.shell_context_processor
    def make_shell_context():
        return dict(db=db, Admin=Admin, Post=Post, Category=Category, Comment=Comment)


def register_errors(app):
    @app.errorhandler(400)
    def bad_request(e):
        return render_template('errors/400.html'), 400
    
    @app.errorhandler(404)
    def page_not_found(e):
        return render_template('errors/404.html'), 404

    @app.errorhandler(500)
    def internal_server_error(e):
        return render_template('errors/500.html'), 500

def register_commands(app):
    @app.cli.command()
    def initdb():
        click.echo("init db.")

    @app.cli.command()
    @click.option('--category', default=10, help='Quantity of categories, default is 10.')
    @click.option('--post', default=50, help='Quantity of posts, default is 50.')
    @click.option('--comment', default=500, help='Quantity of comments, default is 500.')
    def forge(category, post, comment):
        '''构建虚拟的分类、文章、评论'''
        from blueBlog.fakes import fake_admin, fake_categories, fake_posts, fake_comments

        db.drop_all()
        db.create_all()

        click.echo('Generating the administrator...')
        fake_admin()

        click.echo('Generating %d categories...' % category)
        fake_categories(category)

        click.echo('Generating %d posts...' % post)
        fake_posts(post)

        click.echo('Generating %d comments...' % comment)
        fake_comments(comment)
        
        click.echo('Done.')
    
    

def register_template_context(app):
    pass
